package
{
	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	
	/**
	 * ...
	 * @author Erik Molenaar (& Jorden Schoonderwoerd & Matthijs de Wachter?)
	 */
	public class Main extends Sprite 
	{
		[Embed(source = "Boem2@7.png")]
		private var Boem:Class;
		public var boemSheet:SpriteSheet = new SpriteSheet(Boem, 7, 1);
		[Embed(source = "Buten.png")]
		private var ButenUp:Class;
		[Embed(source = "Buten2.png")]
		private var ButenDown:Class;
		public var button:BitmapButton = new BitmapButton(ButenUp, ButenDown, 0, 50);
		
		private var boem:Bitmap = new Bitmap();
		private var boem2:Bitmap = new Bitmap();
		
		private var textfield:TextField = new TextField();
		private var score:int = 0, boemNum:int = 0, price:int = 4, counter:int = 1;
		private var flipped:Boolean;
		
		private var boemArray:Array = new Array();
		
		public function Main():void 
		{
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
			
			addEventListener(Event.ENTER_FRAME, onEnterFrame);
		}
		
		private function init(e:Event = null):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			// entry point
			textfield.text = "I'm a lumberjack and I'm okay!\nWatashi wa ranbaajakku to daijoubu desu yo!\nYou scored: "
								+ score + "   Current price is: " + price;
			textfield.autoSize = TextFieldAutoSize.LEFT;
			addChild(textfield);
			
			button.addEventListener(MouseEvent.CLICK, onButtonClick);
			button.buttonMode = true;
			addChild(button);
			
			boemSheet.drawTile(boemNum);
			boem.bitmapData = boemSheet.canvasBitmapData;
			boem.y = 100;
			addChild(boem);
			
			boem2.visible = false;
		}
		
		private function onEnterFrame(ev:Event):void
		{
			score++;
			textfield.text = "I'm a lumberjack and I'm okay!\nWatashi wa ranbaajakku to daijoubu desu yo!\nYou scored: "
								+ score + "   Current price is: " + price;
			boemSheet.drawTile((score % 14) / 2);
			boem2.bitmapData = boemSheet.canvasBitmapData;
			if (boemArray.length > 0)
				boemArray.forEach( function(b:Bitmap, ...args):void { b.bitmapData = boem2.bitmapData; } );
		}
		
		private function onButtonClick(e:MouseEvent):void
		{
			if (score > price)
			{
				score -= price;
				
				var img:Bitmap = new Bitmap();
				img.bitmapData = boem2.bitmapData;
				
				boemArray.push(img);
				boemArray[boemArray.length - 1].x = (boemArray.length % 8) * 100;
				boemArray[boemArray.length - 1].y = ((int)(boemArray.length / 8) + 1) * 100;
				addChild(boemArray[boemArray.length - 1]);
				
				price = (int)(price * 1.5);
				textfield.text = "I'm a lumberjack and I'm okay!\nWatashi wa ranbaajakku to daijoubu desu yo!\nYou scored: "
									+ score + "   Current price is: " + price;
			}
			
			boemNum++;
			boemSheet.drawTile(boemNum % 7);
			boem.bitmapData = boemSheet.canvasBitmapData;
		}
		
	}
	
}